package com.ztf.gui;

import com.ztf.bean.DataTransfer;
import com.ztf.bean.Message;
import com.ztf.bean.User;
import com.ztf.customer.Customer;

import java.awt.Color;
import java.awt.Component;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.*;

/**
 * 好友列表
 */
public class ImageCellRender extends DefaultListCellRenderer {
	private static final long serialVersionUID = 1L;
	static List<String> names = new ArrayList<String>();

	@Override
	public Component getListCellRendererComponent(JList<? extends Object> list,
			Object value, int index, boolean isSelected, boolean cellHasFocus){
		//instanceof 判断其左边对象是否为其右边类的实例
		String text1;
		User user=null ;
		if(value instanceof User){
			user = (User) value;
			try{
				//a.这里可以用html来控制字体的颜色
				text1 = "<html><body color=\"red\">"+user.getName()+"</body></html>";
				//b.也可以通过设置前景色来改变字体颜色
				setForeground(Color.blue);

				setText(text1);
				//设置背景色
				setBackground(Color.white);
				//System.out.println(user);
				InputStream is= new ByteArrayInputStream(user.getHead());
				BufferedImage image = ImageIO.read(is);
				setIcon(new ImageIcon(image));
				
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		if(isSelected){
			//清除点击
			ChatRoom.chatRoom.list.clearSelection();

			ChatRoom.chatRoom.lblNewLabel.setText(user.getName());
			ChatRoom.chatRoom.isSelected = user;

			//加载当前这个客户的聊天记录
			List<Message> messages = ChatRoom.fr.get(user);

			MsgListModel mo1 = new MsgListModel();
			for (Message message : messages) {
				mo1.addElement(message);
			}
			ChatRoom.chatRoom.list_1.setModel(mo1);

			ChatRoom.chatRoom.repaint();

		}
		return this;
	}
}